//
//  UICustomAlertView.m
//  Vgo
//
//  Created by 21CN Vgo on 10-9-30.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "UICustomAlertView.h"
#import "CmnMacro.h"

@interface UIAlertView()
//- (void)layoutAnimated:(BOOL)fp8;
@end

@implementation UICustomAlertView

@synthesize customView;
@synthesize userCenter;
@synthesize indexNotDismiss;

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        // Initialization code
		userCenter = CGPointMake(0, 0);
		indexNotDismiss = -1;
    }
    return self;
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

- (void)dealloc {
	[customView removeFromSuperview];
	SAFE_RELEASE(customView);
	SAFE_RELEASE(originControlFrames);
    [super dealloc];
}

- (void)setFrame:(CGRect)rect
{
	[super setFrame:rect];
	
	CGPoint pos = userCenter;
	
	if (pos.x == 0)
		pos.x = self.center.x;
	
	if (pos.y == 0)
		pos.y = self.center.y;
	
	self.center = pos;
}

//- (void)layoutAnimated:(BOOL)fp8 {
//	[super layoutAnimated:fp8];
//
//	float customViewExtH = customView.frame.size.height;
//	
//	CGRect frame = CGRectMake(self.frame.origin.x, self.frame.origin.y,
//					   self.frame.size.width,
//					   self.frame.size.height + customViewExtH);
//	
//	[self setFrame:frame];
//	
//	if ([customView superview] == nil)
//	{
//		for (UIView * view in [customView subviews])
//		{
//			if ([view isKindOfClass:[UITextField class]] || [view isKindOfClass:[UITextView class]])
//			{
//				// FIXME: This is a workaround. By uncomment below, UITextFields in tableview will show characters when typing (possible keyboard reponder issue).
//				UITextField * fixfield = [[UITextField alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 0.0f, 0.0f)];
//				[self addSubview:fixfield];
//				[fixfield release];
//				
//				break;
//			}
//		}
//		
//		//originFrame = self.frame;
//
//		[self addSubview:customView];
//	}
//	
//	UIView * lowestView = nil;
//	for (UIView * view in [self subviews])
//	{
//		if ([view isKindOfClass:[UILabel class]])
//		{
//			NSString * text = ((UILabel*)view).text;
//			
//			if ( lowestView == nil
//				|| (text && ![text isEqualToString:@""]
//					&& (lowestView.frame.origin.y + lowestView.frame.size.height < view.frame.origin.y + view.frame.size.height) )
//				)
//			{
//				lowestView = view;
//			}
//		}
//	}
//	
//	CGRect customFrame = CGRectMake(lowestView.frame.origin.x,
//									lowestView.frame.origin.y + lowestView.frame.size.height + 5,
//									lowestView.frame.size.width,
//									customViewExtH);
//	
//	customView.frame = customFrame;
//	
//	for (UIView * view in [self subviews])
//	{
//		if ([view isKindOfClass:[UIControl class]])
//		{
//			view.frame = CGRectMake(view.frame.origin.x,
//									view.frame.origin.y + customViewExtH,
//									view.frame.size.width,
//									view.frame.size.height);
//		}
//	}
//}

- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated
{
	if (buttonIndex == indexNotDismiss)
		return;
	[super dismissWithClickedButtonIndex:buttonIndex animated:animated];
}

- (void)layoutSubviews {
	
	[super layoutSubviews];
	
	if (self.frame.origin.x != 0.0 && !originControlFrames)
	{
		NSMutableDictionary *tmpDict = [NSMutableDictionary dictionary];
		
		for (UIView * view in [self subviews])
		{
			[tmpDict setObject:[NSValue valueWithCGRect:view.frame] forKey:[NSNumber numberWithUnsignedInt:[view hash]]];
		}

		originControlFrames = [tmpDict retain];
		
	}
	
	if (originControlFrames)
	{
		float customViewExtH = customView.frame.size.height;
		
		CGRect frame = CGRectMake(self.frame.origin.x, self.frame.origin.y,
								  self.frame.size.width,
								  self.frame.size.height + customViewExtH);
		
		if (!CGRectEqualToRect(frame, self.frame))
		{
			[self setFrame:frame];
		}
		
		if ([customView superview] == nil)
		{
			for (UIView * view in [customView subviews])
			{
				if ([view isKindOfClass:[UITextField class]] || [view isKindOfClass:[UITextView class]])
				{
					// FIXME: This is a workaround. By uncomment below, UITextFields in tableview will show characters when typing (possible keyboard reponder issue).
					UITextField * fixfield = [[UITextField alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 0.0f, 0.0f)];
					[self addSubview:fixfield];
					[fixfield release];
					
					break;
				}
			}
			
			//originFrame = self.frame;
			
			[self addSubview:customView];

		}
		
		CGRect lowestFrame = CGRectZero;
		for (UIView * view in [self subviews])
		{
			NSNumber * key = [NSNumber numberWithUnsignedInt:[view hash]];
			NSValue * value = [originControlFrames objectForKey:key];
			
			if (value)
			{
				if ([view isKindOfClass:[UILabel class]])
				{
					NSString * text = ((UILabel*)view).text;
					
					CGRect tmpFrame = [value CGRectValue];
					
					if ( CGRectIsNull(lowestFrame)
						|| (text && ![text isEqualToString:@""]
							&& (lowestFrame.origin.y + lowestFrame.size.height < tmpFrame.origin.y + tmpFrame.size.height) )
						)
					{
						lowestFrame = tmpFrame;
					}
				} else if ([view isKindOfClass:[UIControl class]]) {
					CGRect tmpFrame = [value CGRectValue];
					tmpFrame.origin.y += customViewExtH;
					
					view.frame = tmpFrame;
				}
			}
		}
		
		CGRect customFrame = CGRectMake(lowestFrame.origin.x,
										lowestFrame.origin.y + lowestFrame.size.height + 5,
										lowestFrame.size.width,
										customViewExtH);
		
		customView.frame = customFrame;
	}
}

//- (void)layoutSubviews {
//	
//	[super layoutSubviews];
//	if (customView)
//	{
//		if ([customView superview] == nil)
//		{
//			// FIXME: This is a workaround. By uncomment below, UITextFields in tableview will show characters when typing (possible keyboard reponder issue).
//			UITextField * fixfield = [[UITextField alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 0.0f, 0.0f)];
//			[self addSubview:fixfield];
//			[fixfield release];
//			
//			//originFrame = self.frame;
//
//			[self addSubview:customView];
//		}
//
//		CGRect originBounds = self.bounds;
//		
//		float customViewExtH = customView.frame.size.height;
//		
//		CGRect frame = CGRectMake(0,
//						   0,
//						   originFrame.size.width,
//						   originFrame.size.height + customViewExtH);
//		
//		[self setFrame:frame];
//		
//		UIView * lowestView = nil;
//		for (UIView * view in [self subviews])
//		{
//			if ([view isKindOfClass:[UILabel class]])
//			{
//				if ( lowestView == nil || (lowestView.frame.origin.y + lowestView.frame.size.height < view.frame.origin.y + view.frame.size.height) )
//				{
//					lowestView = view;
//				}
//			}
//		}
//		
//		customView.frame = CGRectMake(lowestView.frame.origin.x,
//									  lowestView.frame.origin.y + lowestView.frame.size.height + 2,
//									  lowestView.frame.size.width,
//									  customViewExtH);
//		
//		for (UIView * view in [self subviews])
//		{
//			if ([view isKindOfClass:[UIControl class]])
//			{
//				view.frame = CGRectMake(view.frame.origin.x,
//										view.frame.origin.y + self.bounds.size.height - originBounds.size.height,
//										view.frame.size.width,
//										view.frame.size.height);
//			}
//		}
//	}
//}

@end
